package cn.maihe.elg.operation.web.system;

import cn.maihe.elg.operation.model.enums.AuditStatusEnum;
import cn.maihe.elg.operation.model.enums.BusinessStatusEnum;
import cn.maihe.elg.operation.model.vo.Query;
import cn.maihe.elg.operation.model.vo.system.OrgChangeHistoryVO;
import cn.maihe.elg.operation.repository.entity.OrgChangeHistoryDO;
import cn.maihe.elg.operation.repository.entity.OrganizationDO;
import cn.maihe.elg.operation.service.system.OrgChangeHistoryService;
import cn.maihe.elg.operation.service.system.OrganizationService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author JHWei on 2018-07-13.
 * @ 创建日期：2018-07-13
 */
@Api(value = "机构变更管理接口", tags = {"机构变更管理"})
@RestController
public class OrgChangeHistoryController {

    @Resource
    private OrgChangeHistoryService orgChangeHistoryService;
    @Resource
    private OrganizationService organizationService;


    @ApiOperation(value = "查询机构变更列表", notes = "根据机构编号查询机构变更列表")
    @RequestMapping(value = "/api/orgChangeHistory/list", method = RequestMethod.POST)
    public PageInfo<OrgChangeHistoryDO> findAll(@RequestBody Query query) {
        return orgChangeHistoryService.findOrgChangeHistoryByPage(query);
    }

    @ApiOperation(value = "获取机构变更详情")
    @RequestMapping(value = "/api/orgChangeHistory/{id}", method = RequestMethod.GET)
    public OrgChangeHistoryDO getOrgChangeHistory(@PathVariable Long id) {
        return orgChangeHistoryService.getOrgChangeHistory(id);
    }

    @ApiOperation(value = "新增机构变更")
    @RequestMapping(value = "/api/orgChangeHistory", method = RequestMethod.POST)
    public void createOrgChangeHistory(@RequestBody OrgChangeHistoryVO orgChangeHistoryVO) {
        OrgChangeHistoryDO orgChangeHistoryDO = new OrgChangeHistoryDO();
        BeanUtils.copyProperties(orgChangeHistoryVO, orgChangeHistoryDO);
        OrganizationDO organizationDO = organizationService.getOrganization(orgChangeHistoryDO.getOrgId());
        if(orgChangeHistoryVO.getOperateStatus().equals("0")){
            organizationDO.setAuditStatus(AuditStatusEnum.AUDITED.getValue());
            if(organizationDO.getBusinessStatus().equals(BusinessStatusEnum.CLOSED.getValue())){
                organizationDO.setBusinessStatus(BusinessStatusEnum.OPENED.getValue());
                orgChangeHistoryDO.setOperateType("0");
            }else{
                orgChangeHistoryDO.setOperateType("2");
            }
        }else{
            organizationDO.setAuditStatus(AuditStatusEnum.AUDIT_REJECT.getValue());
        }
        orgChangeHistoryService.createOrgChangeHistory(orgChangeHistoryDO,organizationDO);
    }
}
